package A_review.huawei_10_27;
import java.util.HashSet;
import java.util.Set;


//TODO:2023/10/29 全排列 字母 可适用于有重复的字母  gpt给的答案
public class Test1 {
        public static Set<String> generatePermutations(String s) {

            Set<String> result = new HashSet<>();
            generatePermutationsHelper(s.toCharArray(), 0, result);
            return result;
        }

        private static void generatePermutationsHelper(char[] chars, int index, Set<String> result) {
            if (index == chars.length) {
                result.add(new String(chars));
            } else {
                for (int i = index; i < chars.length; i++) {
                    swap(chars, index, i);
                    generatePermutationsHelper(chars, index + 1, result);
                    swap(chars, index, i); // Restore the original order for backtracking
                }
            }
        }

        private static void swap(char[] chars, int i, int j) {
            char temp = chars[i];
            chars[i] = chars[j];
            chars[j] = temp;
        }

        public static void main(String[] args) {
            String input = "ABA";
            Set<String> permutations = generatePermutations(input);
            for (String permutation : permutations) {
                System.out.println(permutation);
            }
        }
    }


